package cn.szzsi.common.model;

import cn.szzsi.common.model.base.BaseSpecification;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * Created by admin on 2016/12/14.
 */
//@TableBind(tableName = "b_specification", pkName = "id")
public class Specification extends BaseSpecification<Specification> {
    public static final Specification me = new Specification().dao();

    /**
     *根据商品分类获取对应分类的规格值和属性值
     * @param categoryId
     */
    public List<Specification> getAllByCategoryIdAndShopId(String categoryId, String shopId){
        //TODO
        //categoryId暂时不用,后续要改

        List<Specification> ls = Specification.me.find("select * from mall_specification where shopId = ? and status='0'",shopId);
        for(Specification s:ls) {
            s.raiseValueList(s.getStr("id"));
        }
        return ls;
    }

    public List<Specification> getAllSpecificationByProduct(Product product){
        if(StringUtils.isBlank(product.getStr("specificationids"))){
            return null;
        }
        List<Specification> ls = Specification.me.find("select * from mall_specification where id in ('"+ StringUtils.replace(product.getStr("specificationids"),",","','")+"') order by 'order' desc");
        for(Specification s:ls) {
            s.raiseValueList(s.getStr("id"));
        }
        return ls;
    }

    /**
     * 在商品规格列表中加入values
     * @param specificationid
     */
    public void raiseValueList(String specificationid){
        List<SpecificationValue> list = getValuesBysid(specificationid);
        this.put("specificationValues",list);
    }

    public List<SpecificationValue> getValuesBysid(String sid){
        List<SpecificationValue> list = SpecificationValue.dao.find("select * from mall_specification_value where specification_id = ? order by `order` asc",sid);
        return list;
    }

    public String getValues(){
        List<SpecificationValue> list = getValuesBysid(this.getStr("id"));
        String values = "";
        if(list != null) {
            for (SpecificationValue r : list) {
                values += r.getStr("name") + " ";
            }
        }
        this.put("values",values);
        return values;
    }
}
